package com.maoye.ossp.bpm.example.controller;

/**
 * 学生Contorller
 * 
 * @author zhuangwl
 * @date 2013-12-16
 * @version 1.0
 */
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hotent.base.api.query.QueryFilter;
import com.maoye.ossp.biz.example.entity.Student;
import com.maoye.ossp.bpm.example.entity.BusForm;
import com.maoye.ossp.bpm.example.service.IBusFormService;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.excel.ExportExcel;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.att.entity.Attachment;
import com.maoye.ossp.sys.att.service.IAttService;
import com.maoye.ossp.sys.org.util.UserUtils;

@Controller
@RequestMapping(value = "bpm/example/busForm/")
public class BusFormController extends BaseController {
	ArrayList<Attachment> files = new ArrayList<Attachment>();
	@Resource
	private IBusFormService busFormService;

	// @Resource
	// IUPloadService upload;
	@Resource
	IAttService service;

	@RequestMapping(value = "list")
	public String list(BusForm busForm, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		QueryFilter queryFilter = getQuerFilter(request, busForm);
		Page<BusForm> page = (Page<BusForm>) busFormService.find(queryFilter);
		model.addAttribute("page", page);
		return resultPage("/bpm/example/busForm/list", model, request);
	}

	@RequestMapping(value = "save")
	public String save(BusForm student, Model model,
			RedirectAttributes redirectAttributes, HttpServletRequest request,
			HttpServletResponse response) {
		busFormService.create(student);
		if (files != null && files.size() != 0) {
			// TODO 附件功能调整
			// Attachment att = files.get(0);
			// att.setSoureBillId(student.getId());
			// service.update(att);
		}
		addMessage(redirectAttributes, "保存学生'" + student.getName() + "'成功");
		return SUCCESS;
	}
	@RequestMapping(value = "update")
	public String update(BusForm student, Model model,
			RedirectAttributes redirectAttributes, HttpServletRequest request,
			HttpServletResponse response) {
		busFormService.update(student);
		if (files != null && files.size() != 0) {
			// TODO 附件功能调整
			// Attachment att = files.get(0);
			// att.setSoureBillId(student.getId());
			// service.update(att);
		}
		addMessage(redirectAttributes, "保存学生'" + student.getName() + "'成功");
		return SUCCESS;
	}

	@RequestMapping(value = "add")
	public String add(BusForm student, Model model) {
		student.setCreateBy(UserUtils.getCurrentUserId().toString());
		student.setCreateTime(new Date());
		student.setId(idGenerator.getUId());
		student.setVersionUrl("biz/example/student");
		model.addAttribute("student", student);
		return "/bpm/example/busForm/add";
	}

	@RequestMapping(value = "{id}/edit")
	public String edit(@PathVariable Long id, Model model) {
		BusForm student = busFormService.get(id);
		model.addAttribute("student", student);
		return "/bpm/example/busForm/add";
	}

	@RequestMapping(value = "{id}/view")
	public String view(@PathVariable Long id, Model model) {
		BusForm student = busFormService.get(id);
		model.addAttribute("student", student);
		return "/bpm/example/busForm/view";
	}

	@RequestMapping(value = "{id}/delete")
	public String delete(@PathVariable Long id, Model model,
			RedirectAttributes redirectAttributes) {
		busFormService.remove(id);
		addMessage(redirectAttributes, "删除学生信息成功！");
		return "redirect:/bpm/example/busForm/list";
	}

	@RequestMapping(value = "deleteAll")
	public String deleteAll(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String deleteIds = request.getParameter("deleteIds");
		String[] ids = deleteIds.split(";");
		for (String id : ids) {
			busFormService.remove(Long.parseLong(id));
		}
		return "redirect:/bpm/example/busForm/list";
	}

	/***************************************************
	 * 
	 * 
	 * linzhizhong
	 * 
	 * @param request
	 *            : MultipartHttpServletRequest auto passed
	 * @param response
	 *            : HttpServletResponse auto passed
	 * @return LinkedList<FileMeta> as json format
	 ****************************************************/
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public @ResponseBody
	ArrayList<Attachment> upload(MultipartHttpServletRequest request,
			HttpServletResponse response) throws IOException {

		files = new ArrayList<Attachment>();
		Attachment fileMeta = null;
		String savepath = "D:\\temp\\files\\";
		long id = idGenerator.getUId();
		// TODO 附件功能调整
		// files = upload.upload(savepath, request, service, id);
		return files;
	}

	/**
	 * 导出EXcel
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @author liuw
	 * @data 2014年1月24日
	 */
	@RequestMapping(value = "exportExcel")
	public ModelAndView exportExcel(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/excel/exceportToExcel");
		try {
			List<Object> list = new ArrayList<Object>();
			Student s = new Student();
			for (int i = 0; i < 160000; i++) {

				s.setId((long) 0001);
				s.setName("张三is" + i);
				s.setIntro("123");
				list.add(s);
			}
			// List<Student> list = busFormService.getAll();
			ExportExcel.exportExcel("学生表", list, Student.class, request);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}

	/**
	 * FreeMarker
	 */
	@RequestMapping(value = "freeMarker")
	public String freeMarker(Model model, HttpServletRequest request,
			HttpServletResponse response) {
		String word0 = "Hello ";
		String word1 = "World!";
		model.addAttribute("a0", word0);
		model.addAttribute("a1", word1);
		return "helloWorld.ftl";

	}
}
